package com.ruoyi.rpt.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rpt.mapper.RptDefinitionMapper;
import com.ruoyi.rpt.mapper.RptParameterMapper;
import com.ruoyi.rpt.domain.RptDefinition;
import com.ruoyi.rpt.domain.RptParameter;
import com.ruoyi.rpt.service.IRptDefinitionService;

/**
 * 报表定义Service业务层处理
 * 
 */
@Service
public class RptDefinitionServiceImpl implements IRptDefinitionService 
{
    @Autowired
    private RptDefinitionMapper rptDefinitionMapper;

    @Autowired
    private RptParameterMapper rptParameterMapper;

    /**
     * 查询报表定义
     * 
     * @param reportId 报表定义主键
     * @return 报表定义
     */
    @Override
    public RptDefinition selectRptDefinitionByReportId(Long reportId)
    {
        RptDefinition rptDefinition = rptDefinitionMapper.selectRptDefinitionByReportId(reportId);
        if (rptDefinition != null) {
            // 查询报表参数
            List<RptParameter> parameters = rptParameterMapper.selectRptParameterListByReportId(reportId);
            rptDefinition.setParameters(parameters);
        }
        return rptDefinition;
    }

    /**
     * 查询报表定义列表
     * 
     * @param rptDefinition 报表定义
     * @return 报表定义
     */
    @Override
    public List<RptDefinition> selectRptDefinitionList(RptDefinition rptDefinition)
    {
        return rptDefinitionMapper.selectRptDefinitionList(rptDefinition);
    }

    /**
     * 新增报表定义
     * 
     * @param rptDefinition 报表定义
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRptDefinition(RptDefinition rptDefinition)
    {
        rptDefinition.setCreateTime(DateUtils.getNowDate());
        int rows = rptDefinitionMapper.insertRptDefinition(rptDefinition);
        
        // 保存报表参数
        insertRptParameters(rptDefinition);
        
        return rows;
    }

    /**
     * 修改报表定义
     * 
     * @param rptDefinition 报表定义
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRptDefinition(RptDefinition rptDefinition)
    {
        rptDefinition.setUpdateTime(DateUtils.getNowDate());
        
        // 删除原有参数
        rptParameterMapper.deleteRptParameterByReportId(rptDefinition.getReportId());
        
        // 保存新的参数
        insertRptParameters(rptDefinition);
        
        return rptDefinitionMapper.updateRptDefinition(rptDefinition);
    }

    /**
     * 批量删除报表定义
     * 
     * @param reportIds 需要删除的报表定义主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRptDefinitionByReportIds(Long[] reportIds)
    {
        // 删除关联的参数
        for (Long reportId : reportIds) {
            rptParameterMapper.deleteRptParameterByReportId(reportId);
        }
        
        return rptDefinitionMapper.deleteRptDefinitionByReportIds(reportIds);
    }

    /**
     * 删除报表定义信息
     * 
     * @param reportId 报表定义主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRptDefinitionByReportId(Long reportId)
    {
        // 删除关联的参数
        rptParameterMapper.deleteRptParameterByReportId(reportId);
        
        return rptDefinitionMapper.deleteRptDefinitionByReportId(reportId);
    }
    
    /**
     * 保存报表参数
     * 
     * @param rptDefinition 报表定义对象
     */
    private void insertRptParameters(RptDefinition rptDefinition) {
        List<RptParameter> parameters = rptDefinition.getParameters();
        if (parameters != null && !parameters.isEmpty()) {
            for (RptParameter parameter : parameters) {
                parameter.setReportId(rptDefinition.getReportId());
                parameter.setCreateTime(DateUtils.getNowDate());
                rptParameterMapper.insertRptParameter(parameter);
            }
        }
    }
} 